/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UnitListPanel.java
 *
 * Created on 18-mei-2011, 12:42:56
 */
package tasks.ui.architecture;

import common.Observer;
import common.ObserverInterest;
import domain.DomainFacade;
import domain.DomainFacadeImpl;

/**
 *
 * @author Tim Hofman Laptop
 */
public class UnitListPanel extends javax.swing.JPanel implements Observer {

    /** Creates new form UnitListPanel */
    public UnitListPanel() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        unitList = new javax.swing.JList();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Units");

        unitList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                unitListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(unitList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void unitListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_unitListValueChanged

        if (evt.getValueIsAdjusting() == false) {

            if (unitList.getSelectedValue() != null) {
                this.controller.setSelectedUnit(listModel.getUnitAt(unitList.getSelectedIndex()));
            }
        }
    }//GEN-LAST:event_unitListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList unitList;
    // End of variables declaration//GEN-END:variables
    private ArchitectureController controller;
    private UnitListModel listModel;
    private DomainFacade facade;

    public void setController(ArchitectureController controller) {
        this.controller = controller;
        this.controller.addObserver(this, ObserverInterest.LAYER_SELECTION_CHANGED);
    }

    @Override
    public void notifyChange(ObserverInterest interest) {
        if (this.controller.getSelectedLayer() != null) {
            this.listModel = new UnitListModel(this.controller.getUnitsFromLayer(this.controller.getSelectedLayer()));
            unitList.setModel(listModel);
        } else {
            unitList.setListData(new String[0]);
        }

    }

    private void init() {
        this.facade = DomainFacadeImpl.getInstance();
        this.facade.addObserver(this, ObserverInterest.UNITS_CHANGED);
    }
}
